package view.manager;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;

import base.Session;
import dao.ManagerDao;
import model.Manager;
import util.StringUtil;

import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ModifyInfoInterfrm extends JInternalFrame {
	private JTextField textField;
	private JTextField textField_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ModifyInfoInterfrm frame = new ModifyInfoInterfrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ModifyInfoInterfrm() {
		setTitle("修改个人信息");
		setIconifiable(true);
		setClosable(true);
		setBounds(100, 100, 450, 300);
		
		JLabel lblNewLabel = new JLabel("姓 名：");
		lblNewLabel.setIcon(new ImageIcon(ModifyInfoInterfrm.class.getResource("/images/person.png")));
		
		textField = new JTextField();
		textField.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("电 话：");
		lblNewLabel_1.setIcon(new ImageIcon(ModifyInfoInterfrm.class.getResource("/images/电 话.png")));
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		
		JButton btnNewButton = new JButton("确认");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (StringUtil.isEmpty(textField.getText())) {
					JOptionPane.showMessageDialog(null, "姓名不能为空", "提示", JOptionPane.ERROR_MESSAGE);
					textField.requestFocus();
					return;
				}
				if (textField.getText().length() >= 20) {
					JOptionPane.showMessageDialog(null, "姓名太长", "提示", JOptionPane.ERROR_MESSAGE);
					textField.requestFocus();
					textField.selectAll();
					return;
				}
				if (StringUtil.isEmpty(textField_1.getText())) {
					JOptionPane.showMessageDialog(null, "电话不能为空", "提示", JOptionPane.ERROR_MESSAGE);
					textField_1.requestFocus();
					return;
				}
				if (!StringUtil.checkPhone(textField_1.getText())) {
					JOptionPane.showMessageDialog(null, "电话格式错误", "提示", JOptionPane.ERROR_MESSAGE);
					textField_1.requestFocus();
					textField_1.selectAll();
					return;
				}
				Manager manager = ManagerDao.getManager(Session.id);
				manager.setT_name(textField.getText());
				manager.setT_tel(textField_1.getText());
				
				if (ManagerDao.updata(manager)>0) {
					JOptionPane.showMessageDialog(null, "添加成功", "提示", JOptionPane.INFORMATION_MESSAGE);
				}
			}
		});
		btnNewButton.setIcon(new ImageIcon(ModifyInfoInterfrm.class.getResource("/images/confirm.png")));
		
		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField.setText("");
				textField_1.setText("");
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(ModifyInfoInterfrm.class.getResource("/images/resets.png")));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(124)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addComponent(btnNewButton)
							.addPreferredGap(ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
							.addComponent(btnNewButton_1))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(lblNewLabel_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
							.addGap(18)
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addComponent(textField, GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)
								.addComponent(textField_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE))))
					.addGap(142))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(47)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_1))
					.addGap(50)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton_1)
						.addComponent(btnNewButton))
					.addContainerGap(90, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);

	}

}
